﻿namespace AC.ExtendedRenderer.Toolkit
{
    using AC.ExtendedRenderer.Toolkit.Drawing;
    using AC.ExtendedRenderer.Toolkit.Utils;
    using ComponentFactory.Krypton.Toolkit;
    using System;
    using System.ComponentModel;
    using System.Drawing;
    using System.Drawing.Text;
    using System.Windows.Forms;

    [ToolboxBitmap(typeof(ComboBox))]
    public class KryptonComboBox : ComboBox
    {
        private Color _borderColor = Color.Black;
        private bool _disableBorderColor;
        private Color _gradientEndColor = Color.Gray;
        private Color _gradientStartColor = Color.White;
        private IPalette _palette;
        private PaletteRedirect _paletteRedirect;
        private bool _persistentColors;
        private bool _useStyledColors;

        public KryptonComboBox()
        {
            base.SetStyle(ControlStyles.OptimizedDoubleBuffer | ControlStyles.AllPaintingInWmPaint, true);
            base.UpdateStyles();
            base.DrawMode = DrawMode.OwnerDrawVariable;
            if (this._palette != null)
            {
                this._palette.PalettePaint += new EventHandler<PaletteLayoutEventArgs>(this.OnPalettePaint);
            }
            KryptonManager.GlobalPaletteChanged += new System.EventHandler(this.OnGlobalPaletteChanged);
            this._palette = KryptonManager.CurrentGlobalPalette;
            this._paletteRedirect = new PaletteRedirect(this._palette);
            this._borderColor = this._palette.ColorTable.ButtonSelectedBorder;
            this.InitializeComponent();
        }

        protected override void Dispose(bool disposing)
        {
            if (disposing)
            {
                if (this._palette != null)
                {
                    this._palette.PalettePaint -= new EventHandler<PaletteLayoutEventArgs>(this.OnPalettePaint);
                    this._palette = null;
                }
                KryptonManager.GlobalPaletteChanged -= new System.EventHandler(this.OnGlobalPaletteChanged);
            }
            base.Dispose(disposing);
        }

        public void DrawBorder(ref Message message, int width, int height)
        {
            if (((message.Msg == 0x85) || (message.Msg == 20)) || (message.Msg == 15))
            {
                IntPtr hdc = Win32.GetDCEx(message.HWnd, (IntPtr) 1, 0x21);
                if (hdc != IntPtr.Zero)
                {
                    Graphics graphics = Graphics.FromHdc(hdc);
                    Rectangle bounds = new Rectangle(0, 0, width, height);
                    ControlPaint.DrawBorder(graphics, bounds, this._borderColor, ButtonBorderStyle.Solid);
                    message.Result = (IntPtr) 1;
                    Win32.ReleaseDC(message.HWnd, hdc);
                }
            }
        }

        private void InitializeComponent()
        {
            base.SuspendLayout();
            base.ResumeLayout(false);
        }

        protected override void OnDrawItem(DrawItemEventArgs e)
        {
            Rectangle bounds = e.Bounds;
            Graphics g = e.Graphics;
            if (e.Index != -1)
            {
                try
                {
                    e.Graphics.TextRenderingHint = TextRenderingHint.ClearTypeGridFit;
                }
                catch (Exception exception)
                {
                    Console.Write(exception.Message);
                }
                if (this._palette == null)
                {
                    EventArgs args = new EventArgs();
                    this.OnGlobalPaletteChanged(this, args);
                }
                if (!this._persistentColors)
                {
                    if (this._useStyledColors)
                    {
                        this._gradientStartColor = Color.FromArgb(0xff, 0xf6, 0xd7);
                        this._gradientEndColor = Color.FromArgb(0xff, 0xd5, 0x4d);
                    }
                    else
                    {
                        this._gradientStartColor = this._palette.ColorTable.StatusStripGradientBegin;
                        this._gradientEndColor = this._palette.ColorTable.OverflowButtonGradientEnd;
                    }
                }
                Color darkColor = this._gradientStartColor;
                Color lightColor = this._gradientEndColor;
                Color statusStripText = this._palette.ColorTable.StatusStripText;
                Font font = e.Font;
                if (font == null)
                {
                    font = this.Font;
                }
                e.DrawBackground();
                string s = (string) base.Items[e.Index];
                e.Graphics.DrawString(s, font, new SolidBrush(this.ForeColor), e.Bounds);
                if ((e.State & DrawItemState.Selected) != DrawItemState.None)
                {
                    DrawingMethods.DrawGradient(g, e.Bounds, darkColor, lightColor, 90f, false, Color.White, 0f);
                    e.Graphics.DrawString(s, font, new SolidBrush(statusStripText), e.Bounds);
                    e.DrawFocusRectangle();
                }
                e.DrawFocusRectangle();
            }
        }

        private void OnGlobalPaletteChanged(object sender, EventArgs e)
        {
            if (this._palette != null)
            {
                this._palette.PalettePaint -= new EventHandler<PaletteLayoutEventArgs>(this.OnPalettePaint);
            }
            this._palette = KryptonManager.CurrentGlobalPalette;
            this._paletteRedirect.Target = this._palette;
            if (this._palette != null)
            {
                this._palette.PalettePaint += new EventHandler<PaletteLayoutEventArgs>(this.OnPalettePaint);
                if (!this._persistentColors)
                {
                    if (this._useStyledColors)
                    {
                        this._gradientStartColor = Color.FromArgb(0xff, 0xf6, 0xd7);
                        this._gradientEndColor = Color.FromArgb(0xff, 0xd5, 0x4d);
                    }
                    else
                    {
                        this._gradientStartColor = this._palette.ColorTable.StatusStripGradientBegin;
                        this._gradientEndColor = this._palette.ColorTable.OverflowButtonGradientEnd;
                    }
                }
            }
            base.Invalidate();
        }

        protected override void OnMeasureItem(MeasureItemEventArgs e)
        {
            base.OnMeasureItem(e);
        }

        protected override void OnPaint(PaintEventArgs e)
        {
            base.OnPaint(e);
        }

        protected override void OnPaintBackground(PaintEventArgs pevent)
        {
            base.OnPaintBackground(pevent);
        }

        private void OnPalettePaint(object sender, PaletteLayoutEventArgs e)
        {
            base.Invalidate();
        }

        protected override void WndProc(ref Message m)
        {
            base.WndProc(ref m);
            if (!this._disableBorderColor)
            {
                this.DrawBorder(ref m, base.Width, base.Height);
            }
        }

        [DefaultValue("Color.Black"), DesignerSerializationVisibility(DesignerSerializationVisibility.Hidden), Browsable(true), Category("Appearance-Extended")]
        public Color BorderColor
        {
            get
            {
                return this._borderColor;
            }
            set
            {
                this._borderColor = value;
                base.Invalidate();
            }
        }

        [DefaultValue("False"), Browsable(true), Category("Appearance-Extended")]
        public bool DisableBorderColor
        {
            get
            {
                return this._disableBorderColor;
            }
            set
            {
                this._disableBorderColor = value;
            }
        }

        [DesignerSerializationVisibility(DesignerSerializationVisibility.Hidden), Browsable(true), Category("Appearance-Extended"), DefaultValue("Color.Gray")]
        public Color GradientEndColor
        {
            get
            {
                return this._gradientEndColor;
            }
            set
            {
                this._gradientEndColor = value;
                base.Invalidate();
            }
        }

        [Category("Appearance-Extended"), DefaultValue("Color.White"), DesignerSerializationVisibility(DesignerSerializationVisibility.Hidden), Browsable(true)]
        public Color GradientStartColor
        {
            get
            {
                return this._gradientStartColor;
            }
            set
            {
                this._gradientStartColor = value;
                base.Invalidate();
            }
        }

        [DefaultValue("False"), Category("Appearance-Extended"), Browsable(true)]
        public bool PersistentColors
        {
            get
            {
                return this._persistentColors;
            }
            set
            {
                this._persistentColors = value;
            }
        }

        [Browsable(true), DefaultValue("False"), Category("Appearance-Extended")]
        public bool UseStyledColors
        {
            get
            {
                return this._useStyledColors;
            }
            set
            {
                this._useStyledColors = value;
            }
        }
    }
}

